import uvicorn
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from starlette.staticfiles import StaticFiles

app = FastAPI()
templates = Jinja2Templates(directory="templates")
# 挂载目录
app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
    return templates.TemplateResponse("login.html", {"request": request})

@app.post("/submit_form")
async def submit_form(userid: str = Form(...), password: str = Form(...)):
    success = True
    # TODO
    error_msg = ""

    if success:
        return {"success": 0}
    else:
        return {"success": -1, "error_msg": error_msg}


# @app.get("/book/reg", response_class=HTMLResponse)
# async def register(request: Request):
#     return templates.TemplateResponse("registration.html", {"request": request})


if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
